//
//  TMMineController.m
//  国学商家管理
//
//  Created by Tao Li on 2018/6/28.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMMineController.h"
#import "TMLoginController.h"
#import "TMUserInformationController.h"
#import "TMToCommentController.h"
#import "TMUserIconController.h"
#import "TMShoppingCartController.h"
#import "TMInvitationController.h"
#import "TMSystemsMessController.h"
#import "TMMerchantCollectController.h"
#import "TMTradeCollectController.h"
#import "TMVipCenterController.h"
#import "TMMessageCenterController.h"
#import "TMMyDiscountViewController.h"
#import "UILabel+TMColorText.h"
#import "TMUserViewModel.h"
#import "TMUserInformationModel.h"
#import "TMConversationListController.h"
#import "STWebViewController.h"
@interface TMMineController ()
@property (weak, nonatomic) IBOutlet UIImageView *userIconImageV;
@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UILabel *vipLabel;
@property (weak, nonatomic) IBOutlet UIImageView *applyImageView;
@property (weak, nonatomic) IBOutlet UILabel *applyLabel;
@property (weak, nonatomic) IBOutlet UIButton *applyInBtn;
@property (weak, nonatomic) IBOutlet UIView *willCompleteImage;
@property (weak, nonatomic) IBOutlet UIView *willCommentImage;
@property (weak, nonatomic) IBOutlet UIView *didCompleteImage;
@property (weak, nonatomic) IBOutlet UIView *refundImage;
@property (weak, nonatomic) IBOutlet UILabel *commityCollectionLabel;
@property (weak, nonatomic) IBOutlet UILabel *shopCollectionLabel;
@property (weak, nonatomic) IBOutlet UILabel *recordsCollectionLabel;
@property (nonatomic, strong) TMUserViewModel *viewModel;
@property (weak, nonatomic) IBOutlet UILabel *cornerLabel1;
@property (weak, nonatomic) IBOutlet UILabel *cornerLabel2;
@property (weak, nonatomic) IBOutlet UILabel *cornerLabel3;
@property (weak, nonatomic) IBOutlet UILabel *cornerLabel4;
@property (nonatomic, strong) TMUserInformationModel *userIoforModel;
@end

@implementation TMMineController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //[self.tableView.mj_header beginRefreshing];
    [self getdata];
    int allunread = [[RCIMClient sharedRCIMClient] getTotalUnreadCount];//获取消息数量
    if (allunread > 0 ) {
        [self displayRightItem:@"图iOS"];
    }else{
        [self displayRightItem:@"信息11"];
    }
    self.vipLabel.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的";
    self.viewModel = [[TMUserViewModel alloc]init];
    [self displayCorners];
    [self displayImageActions];
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getdata];
    //}];
    //[self.tableView.mj_header beginRefreshing];
    
}
- (void)getdata{
    [self.viewModel getMyPageDataSuccess:^(id responseObject) {
        self.userIoforModel = responseObject;
        [self displayTabbleview:responseObject];
//        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    } failed:^(NSString *error) {
        //[self.tableView.mj_header endRefreshing];
    }];
}
- (void)displayRightItem:(NSString *)name{
    UIBarButtonItem *letfItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"设置"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(gotoPersonInformation)];
   UIBarButtonItem *letfItem1 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:name] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(messageDetail)];
    self.navigationItem.rightBarButtonItems = @[letfItem1,letfItem];
}

- (void)displayTabbleview:(TMUserInformationModel *)model{
    
    self.userIconImageV.contentMode = UIViewContentModeScaleAspectFill;
    self.userIconImageV.clipsToBounds = YES;
    [self.userIconImageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithBaseUrl:model.userimage]] placeholderImage:[UIImage imageNamed:@"用户头像"]];
    self.userName.text = model.username;
    [TMUserManager deaufatManager].userModel.userimage = model.userimage;
    [TMUserManager deaufatManager].userModel.userName = model.username;
    self.shopCollectionLabel.text = [NSString stringWithFormat:@"%ld", model.coll_shop];
    self.commityCollectionLabel.text = [NSString stringWithFormat:@"%ld", model.coll_goods];
    self.recordsCollectionLabel.text = [NSString stringWithFormat:@"%ld", model.history];
    self.cornerLabel1.hidden = (model.over == 0) ?YES :NO;
    self.cornerLabel3.hidden = (model.tip == 0) ?YES :NO;
    self.cornerLabel2.hidden = YES;
    //self.cornerLabel2.hidden = (model.com == 0) ?YES :NO;
    self.cornerLabel4.hidden = (model.ret == 0) ?YES :NO;
    self.cornerLabel1.text = [NSString stringWithFormat:@"%ld", model.over];
    self.cornerLabel3.text = [NSString stringWithFormat:@"%ld", model.tip];
    self.cornerLabel2.text = [NSString stringWithFormat:@"%ld", model.com];
    self.cornerLabel4.text = [NSString stringWithFormat:@"%ld", model.ret];
    self.vipLabel.text = [NSString stringWithFormat:@"会员等级:%@",model.rank];
    [self.vipLabel displayTextWithText:self.vipLabel.text AndRange:NSMakeRange(5, self.vipLabel.text.length-5) AndFont:12 AndColor:[UIColor hx_colorWithHexRGBAString:@"FFB22C"]];

}


- (void)displayCorners{
    self.cornerLabel1.hidden = YES;
    self.cornerLabel2.hidden = YES;
    self.cornerLabel3.hidden = YES;
    self.cornerLabel4.hidden = YES;
    self.cornerLabel1.layer.cornerRadius = CGRectGetWidth(self.cornerLabel1.bounds)/2;
    self.cornerLabel1.layer.masksToBounds = YES;
    self.cornerLabel2.layer.cornerRadius = CGRectGetWidth(self.cornerLabel2.bounds)/2;
    self.cornerLabel2.layer.masksToBounds = YES;
    self.cornerLabel3.layer.cornerRadius = CGRectGetWidth(self.cornerLabel3.bounds)/2;
    self.cornerLabel3.layer.masksToBounds = YES;
    self.cornerLabel4.layer.cornerRadius = CGRectGetWidth(self.cornerLabel4.bounds)/2;
    self.cornerLabel4.layer.masksToBounds = YES;
}
- (void)gotoPersonInformation{
    NSLog(@"保存成功");
    if ([TMUserManager deaufatManager].userModel.token == nil) {
        MUST_LOGIN_ALERT;
        return;
    }
    TMUserInformationController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMUserInformationController"];
    vc.model = self.userIoforModel;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)messageDetail{
    NSLog(@"保存成功");
    TMConversationListController *vc = [[TMConversationListController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)displayImageActions{
    UITapGestureRecognizer *iconTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(iconTapAction:)];
    [self.userIconImageV addGestureRecognizer:iconTap];
    
    UITapGestureRecognizer *iconTap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(willCompleteTapAction:)];
    [self.willCompleteImage addGestureRecognizer:iconTap1];
    
    UITapGestureRecognizer *aply = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(applyInAction:)];
    [self.applyImageView addGestureRecognizer:aply];
    UITapGestureRecognizer *iconTap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(willCommentTapAction:)];
    [self.didCompleteImage addGestureRecognizer:iconTap2];
    //willCommentTapAction
    UITapGestureRecognizer *iconTap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didCompleteTapAction:)];
    [self.willCommentImage addGestureRecognizer:iconTap3];
    UITapGestureRecognizer *iconTap4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(refundTapAction:)];
    [self.refundImage addGestureRecognizer:iconTap4];
    UITapGestureRecognizer *iconTap5 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(vipLabelTapAction:)];
    [self.vipLabel addGestureRecognizer:iconTap5];
}
#pragma --mark apply in
- (void)applyInAction:(UITapGestureRecognizer *)tap{
    
}
- (void)iconTapAction:(UITapGestureRecognizer *)tap{
    
//    if ([TMUserManager deaufatManager].userModel.token) {
//    TMUserInformationController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMUserInformationController"];
//        vc.model = self.userIoforModel;
//        [self.navigationController pushViewController:vc animated:YES];
//    }else{
//    TMLoginController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMLoginController"];
//    [self.navigationController pushViewController:vc animated:YES];
//    }
}
- (void)willCompleteTapAction:(UITapGestureRecognizer *)tap{
    if ([TMUserManager deaufatManager].userModel.token == nil) {
        MUST_LOGIN_ALERT;
        return;
    }
    TMToCommentController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMToCommentController"];
    vc.compelteStateSet = CompelteStateWait;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didCompleteTapAction:(UITapGestureRecognizer *)tap{
    if ([TMUserManager deaufatManager].userModel.token == nil) {
        MUST_LOGIN_ALERT;
        return;
    }
    TMToCommentController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMToCommentController"];
    vc.compelteStateSet = CompelteStateOver;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)willCommentTapAction:(UITapGestureRecognizer *)tap{
    if ([TMUserManager deaufatManager].userModel.token == nil) {
        MUST_LOGIN_ALERT;
        return;
    }
    TMToCommentController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMToCommentController"];
    vc.compelteStateSet = CompelteStateComment;
    [self.navigationController pushViewController:vc animated:YES];
}
//退款
- (void)refundTapAction:(UITapGestureRecognizer *)tap{
    if ([TMUserManager deaufatManager].userModel.token == nil) {
        MUST_LOGIN_ALERT;
        return;
    }
    TMToCommentController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMToCommentController"];
    vc.compelteStateSet = CompelteStateRefund;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)vipLabelTapAction:(UITapGestureRecognizer *)tap{
    if ([TMUserManager deaufatManager].userModel.token == nil) {
        MUST_LOGIN_ALERT;
        return;
    }
    TMVipCenterController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMVipCenterController"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)commodityCollectionAction:(UIButton *)sender {
    if ([TMUserManager deaufatManager].userModel.token == nil) {
        MUST_LOGIN_ALERT;
        return;
    }
    TMTradeCollectController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMTradeCollectController"];
    vc.isMyCourse = NO;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)shopCollectionAction:(UIButton *)sender {
    if ([TMUserManager deaufatManager].userModel.token == nil) {
        MUST_LOGIN_ALERT;
        return;
    }
    TMMerchantCollectController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMMerchantCollectController"];
    vc.isFoot = NO;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)recordsCollectionAction:(UIButton *)sender{
    if ([TMUserManager deaufatManager].userModel.token == nil) {
        MUST_LOGIN_ALERT;
        return;
    }
    TMMerchantCollectController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMMerchantCollectController"];
    vc.isFoot = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
        //我的课程
        if ([TMUserManager deaufatManager].userModel.token == nil) {
            MUST_LOGIN_ALERT;
            return;
        }
        TMTradeCollectController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMTradeCollectController"];
        vc.isMyCourse = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.row == 4) {
        //
        if ([TMUserManager deaufatManager].userModel.token == nil) {
            MUST_LOGIN_ALERT;
            return;
        }
        TMShoppingCartController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMShoppingCartController"];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.row == 5) {
        //
        if ([TMUserManager deaufatManager].userModel.token == nil) {
            MUST_LOGIN_ALERT;
            return;
        }
        TMMyDiscountViewController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMMyDiscountViewController"];
        vc.isMine = YES;
        vc.distype = WhereDiscountList;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.row == 6) {
        //
        if ([TMUserManager deaufatManager].userModel.token == nil) {
            MUST_LOGIN_ALERT;
            return;
        }
        TMInvitationController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMInvitationController"];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if(indexPath.row == 7){
        [self sendOtherRequest];
    }
    if (indexPath.row == 8) {
        //
        if ([TMUserManager deaufatManager].userModel.token == nil) {
            MUST_LOGIN_ALERT;
            return;
        }
        TMMessageCenterController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMMessageCenterController"];
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark --NetWork Method
- (void)sendOtherRequest{
    NSDictionary *dic = @{@"uid":@([TMUserManager deaufatManager].userModel.uid),@"token":[TMUserManager deaufatManager].userModel.token};
    [[TMDataRequest defaultRequest] requestWithPath:@"per/ser" parameters:dic method:TMDataRequestTypeGet prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSString * url = responseObject[@"url"];
        STWebViewController * vc = [[STWebViewController alloc] initWithUrl:url];
        [self.navigationController pushViewController:vc animated:YES];
        //清除本地数据/
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        [TMProgressHUD showErrorWithStyleDarkStatus:errorMessage];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [TMProgressHUD showErrorWithStyleDarkStatus:@"网络错误！"];
    }];
}
@end
